package com.googlecode.nacl_optimizer.elf.headers;
import java.util.Arrays;

public class Ident {
    public static final int EI_NIDENT_LENGTH = 16;

    enum FileClass {
        ELFCLASSNONE, ELFCLASS32, ELFCLASS64,
    }

    private FileClass fileClass;
    private DataEncoding dataEnc;
    private int fileVersion;

    public Ident(byte[] bs) {
        if (bs.length != EI_NIDENT_LENGTH) {
            throw new IllegalArgumentException();
        }
        byte[] magic = Arrays.copyOfRange(bs, 0, 4);
        if (!Arrays.equals(magic, new byte[] { 0x7f, 'E', 'L', 'F' })) {
            throw new IllegalArgumentException("Bad magic in ident");
        }
        switch (bs[4]) {
        case 0:
            fileClass = FileClass.ELFCLASSNONE;
            break;
        case 1:
            fileClass = FileClass.ELFCLASS32;
            break;
        case 2:
            fileClass = FileClass.ELFCLASS64;
            break;
        }

        switch (bs[5]) {
        case 0:
            dataEnc=DataEncoding.ELFDATANONE;
            break;
        case 1:
            dataEnc=DataEncoding.ELFDATA2LSB;
            break;
        case 2:
            dataEnc=DataEncoding.ELFDATA2MSB;
            break;
        }

        fileVersion = bs[6];
    }

    public DataEncoding getEncoding() {
        return dataEnc;
    }

    @Override
    public String toString() {
        return "Ident {" + "class: " + fileClass + ", " + "data: " + dataEnc
                + ", " + "version: " + fileVersion + "}";
    }
}
